#!/bin/sh
#chkconfig:2345 80 90
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.


REDIS_BIN_DIR=/Documents/PSM70-linux-run-Standalone/init/Redis_init/../../bin/RedisServer
REDIS_DATA_DIR=/Documents/PSM70-linux-run-Standalone/init/Redis_init/../../bin/RedisServer

EXEC=$REDIS_BIN_DIR/redis-server
CLIEXEC=$REDIS_BIN_DIR/redis-cli

PIDFILE=$REDIS_DATA_DIR/redis_${REDISPORT}.pid
CONF=$REDIS_BIN_DIR/redis.conf

REDISPORT=$(sed -n '/^port/p' ${CONF}|awk -F" " '{print $NF}')
# 读取端口，读不到就设置默认值6379
if test -z ${REDISPORT}
then
    REDISPORT=6379
fi

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
		cd $REDIS_BIN_DIR
                echo "Starting Redis server..."
                $EXEC $CONF &
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=`cat $PIDFILE`
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
